package com.sky.controller.admin;

import com.sky.dto.GoodsSalesDTO;
import com.sky.mapper.OrderMapper;
import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/report")
@Slf4j
public class ReportController {


    @Autowired
    private ReportService reportService;
    
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 营业额数据统计
     *
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("/turnoverStatistics")
    @ApiOperation("营业额数据统计")
    public Result<TurnoverReportVO> turnoverStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate end) {
        return Result.success(reportService.getTurnover(begin, end));
    }




    //订单统计
    @GetMapping("/ordersStatistics")
    public Result<OrderReportVO> orderStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate end){
        return Result.success(reportService.getOrderStatistics(begin, end));
    }

    //用户统计
    @GetMapping("/userStatistics")
    public Result<UserReportVO> userStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate end) {
        return Result.success(reportService.getUserStatistics(begin, end));

    }


    //查询top10
    @GetMapping("/top10")
    public Result<SalesTop10ReportVO> top10(
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate end) {
        log.info("接收到top10统计请求: begin={}, end={}", begin, end);
        SalesTop10ReportVO result = reportService.getTop10(begin, end);
        log.info("top10统计结果: nameList={}, numberList={}", result.getNameList(), result.getNumberList());
        return Result.success(result);
    }
    
    //测试数据库连接和数据
    @GetMapping("/testData")
    public Result<Map<String, Object>> testData() {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 测试订单表
            Integer orderCount = orderMapper.countByMap(new HashMap<>());
            result.put("orderCount", orderCount);
            
            // 测试订单详情表
            List<GoodsSalesDTO> salesData = orderMapper.getSalesTop10(
                LocalDateTime.of(2020, 1, 1, 0, 0),
                LocalDateTime.of(2030, 12, 31, 23, 59)
            );
            result.put("salesDataCount", salesData != null ? salesData.size() : 0);
            result.put("salesData", salesData);
            
            return Result.success(result);
        } catch (Exception e) {
            result.put("error", e.getMessage());
            return Result.error("测试失败: " + e.getMessage());
        }
    }
    
    //插入测试数据
    @GetMapping("/insertTestData")
    public Result<String> insertTestData() {
        try {
            // 检查是否已有测试数据
            Map<String, Object> params = new HashMap<>();
            params.put("begin", LocalDateTime.of(2025, 10, 1, 0, 0));
            params.put("end", LocalDateTime.of(2025, 10, 31, 23, 59));
            
            List<GoodsSalesDTO> existingData = orderMapper.getSalesTop10(
                LocalDateTime.of(2025, 10, 1, 0, 0),
                LocalDateTime.of(2025, 10, 31, 23, 59)
            );
            
            if (existingData != null && existingData.size() > 0) {
                return Result.success("已有测试数据，无需插入");
            }
            
            // 如果没有测试数据，返回提示信息
            return Result.success("请手动执行add_completed_orders.sql脚本插入测试数据");
        } catch (Exception e) {
            return Result.error("插入测试数据失败: " + e.getMessage());
        }
    }
}